#import "SEGHeadPartView.h"

#define toppadding (LYIsiPhoneX() ? 87 : 72)

@implementation SEGHeadPartViewModel
-(instancetype)init{
    if (self == [super init]) {
        _nickName = @"";
        _describe = @"";
        _icon = @"";
        _sex = @"";
        _authentication = @"";
        _level = @"";
        _address = @"";
        _alterMessage = @"";
        _isOthers = NO;
    }
    return self;
}
@end

@implementation SEGHeadPartViewLayout
- (instancetype)initWithData:(SEGHeadPartViewModel *)data type:(SEGEnumHeadPartViewType)type{
    if (self == [super init]) {
        _data = data;
        _type = type;
    }
    [self segHeadPartViewLayout];
    return self;
}

-(void)segHeadPartViewLayout{
    //高度不固定
    _nickNameLabelHeight = 0;
    _nickNameLabelTextLayout = nil;
    _describeLabelHeight = 0;
    _describeLabelTextLayout = nil;
    _addressLabelHeight = 0;
    _addressLabelTextLayout = nil;
    
    if (!_data) return;
    
    [self layout];
    
    _totalHeight = 0;
    _totalHeight = toppadding + _nickNameLabelHeight;
    _totalHeight += 10;
    _totalHeight +=_describeLabelHeight;
    _totalHeight += 15;
    _totalHeight += 90;
    _totalHeight += 13;
    _totalHeight += _addressLabelHeight;
    _totalHeight += 20;
    _totalHeight += 5; //白色背景阴影10 - 5
 
}

//数据处理
-(void)layout{
    [self segNickNameFontSize:22 color:kColor_1];
    
    [self segDescribeFontSize:13 color:kColor_2];
    
    [self segAddressFontSize:13 color:kColor_3];
}

-(void)segNickNameFontSize:(CGFloat)fontSize color:(UIColor*)color{
    UIFont *font = [UIFont systemFontOfSize:fontSize];//[UIFont fontWithName:@"Heiti SC" size:fontSize];
    NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:_data.nickName];
    text.yy_font = font;
    text.yy_color = color;
    text.yy_strokeWidth = @(-4);
    
    SEGContentTextLinePositionModifier *modifier = [SEGContentTextLinePositionModifier new];
    modifier.font = font;
    modifier.paddingTop = 1;
    modifier.paddingBottom = 1;
    
    SEGTextContainer *container = [SEGTextContainer new];
    container.size = CGSizeMake(kHomePageHeadViewWidth - 31 - 20, HUGE); //高度一行
    container.linePositionModifier = modifier;
    container.maximumNumberOfRows = 1;
    container.truncationType = SEGTextTruncationTypeEnd;
    
    _nickNameLabelTextLayout = [SEGTextLayout layoutWithContainer:container text:text];
    if(!_nickNameLabelTextLayout)return;
    _nickNameLabelHeight = [modifier heightForLineCount:_nickNameLabelTextLayout.rowCount];
}

-(void)segDescribeFontSize:(CGFloat)fontSize color:(UIColor*)color{
    UIFont *font = [UIFont systemFontOfSize:fontSize];//[UIFont fontWithName:@"Heiti SC" size:fontSize];
    NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:_data.describe];
    text.yy_font = font;
    text.yy_color = color;
    
    SEGContentTextLinePositionModifier *modifier = [SEGContentTextLinePositionModifier new];
    modifier.font = font;
    modifier.paddingTop = 0.5;
    modifier.paddingBottom = 0.5;
    
    SEGTextContainer *container = [SEGTextContainer new];
    container.size = CGSizeMake(kHomePageHeadViewWidth - 31 - 20, HUGE);//高度两行
    container.linePositionModifier = modifier;
    container.maximumNumberOfRows = 2;
    container.truncationType = SEGTextTruncationTypeEnd;
    
    _describeLabelTextLayout = [SEGTextLayout layoutWithContainer:container text:text];
    if(!_describeLabelTextLayout)return;
    _describeLabelHeight = [modifier heightForLineCount:_describeLabelTextLayout.rowCount];
    
}

-(void)segAddressFontSize:(CGFloat)fontSize color:(UIColor*)color{
    UIFont *font = [UIFont systemFontOfSize:fontSize];//[UIFont fontWithName:@"Heiti SC" size:fontSize];
    NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:_data.address];
    text.yy_font = font;
    text.yy_color = color;
    
    UIImage *image = [UIImage imageWithContentsOfFile:SEGPicName(@"icon_personal_location")];
    NSAttributedString *img = [SEGKitHelper segAttachmentWithImage:image FontSize:fontSize contentInsets:UIEdgeInsetsMake(0, 0, 0, 3)];
    if (img) {
        [text insertAttributedString:img atIndex:0];
    }
    
    SEGContentTextLinePositionModifier *modifier = [SEGContentTextLinePositionModifier new];
    modifier.font = font;
    modifier.paddingTop = 2;
    modifier.paddingBottom = 0.5;
    
    SEGTextContainer *container = [SEGTextContainer new];
    container.size = CGSizeMake(kHomePageHeadViewWidth - 30, HUGE);//高度两行
    container.linePositionModifier = modifier;
    container.maximumNumberOfRows = 1;
    
    _addressLabelTextLayout = [SEGTextLayout layoutWithContainer:container text:text];
    if(!_addressLabelTextLayout)return;
    _addressLabelHeight = [modifier heightForLineCount:_addressLabelTextLayout.rowCount];
}
@end

@implementation SEGHeadPartView
- (instancetype)initWithFrame:(CGRect)frame {
    if (frame.size.width == 0 && frame.size.height == 0) {
        frame.size.width = [UIScreen mainScreen].bounds.size.width;
        frame.size.height = kHeadPartViewHeight;
    }
    self = [super initWithFrame:frame];

    [self addSubview:self.imageBGView];
    [self.imageBGView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.mas_equalTo(self);
    }];
    [self addSubview:self.nickNameLabel];
    [self addSubview:self.describeLabel];
    [self addSubview:self.whiteBGView];
    //白色方块背景
    [self.whiteBGView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(166);
        make.left.equalTo(self).mas_offset(7.5);
        make.right.equalTo(self).mas_offset(-7.5);
        make.height.mas_equalTo(118);

    }];
    [self addSubview:self.iconButton];
    [self.iconButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.whiteBGView).mas_offset(-18);
        make.left.equalTo(self).mas_offset(30);
        make.width.height.mas_equalTo(90);
    }];
    [self addSubview:self.alterMessageButton];
    [self.whiteBGView  addSubview:self.sexImage];
    [self.whiteBGView  addSubview:self.authImage];
    [self.whiteBGView  addSubview:self.levelImage];
    [self.whiteBGView  addSubview:self.redDot];
    [self.whiteBGView  addSubview:self.addressLabel];
    
    // if (kDebug)self.backgroundColor = kSegRandomColor;
    return self;
}

-(void)setWithLayout:(SEGHeadPartViewLayout *)layout{
    self.height = layout.totalHeight;
    _layout = layout;
    
    if (self.height <= 0) return;
    
    [self.iconButton sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",kClientImageRootURL,_layout.data.icon]] forState:UIControlStateNormal placeholderImage:[UIImage imageWithContentsOfFile:SEGPicName(@"female_heading180")]];
    [self.levelImage setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",kClientImageRootURL,_layout.data.level]] placeholderImage:nil];
    self.levelImage.hidden = (_layout.data.level.length == 0);
    [self.sexImage setImage:[UIImage imageWithContentsOfFile:SEGPicName(_layout.data.sex)]];
    [self.authImage setImage:[UIImage imageWithContentsOfFile:SEGPicName(_layout.data.authentication)]];

    self.addressLabel.textLayout = layout.addressLabelTextLayout;
    self.nickNameLabel.textLayout = layout.nickNameLabelTextLayout;
    self.describeLabel.textLayout = layout.describeLabelTextLayout;
    
    [self setupUI];
}

-(void)setupUI {
    [self.imageBGView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.mas_equalTo(self);
    }];
    
    [self.nickNameLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).mas_offset(toppadding);
        make.left.equalTo(self).mas_offset(31);
        make.height.mas_equalTo(_layout.nickNameLabelHeight);
        make.width.mas_equalTo(kHomePageHeadViewWidth - 31 - 20);
    }];
    
    [self.describeLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.nickNameLabel.mas_bottom).mas_offset(10);
        make.left.equalTo(self).mas_offset(31);
        make.height.mas_equalTo(_layout.describeLabelHeight);
        make.width.mas_equalTo(kHomePageHeadViewWidth - 31 - 20);
    }];
    
    //是否为别人主页的布局
    [self isOthersHomePage];
}

-(void)isOthersHomePage{
    if (_layout.data.isOthers == YES) {//别人
        self.alterMessageButton.hidden = YES;
        self.redDot.hidden = YES;
    }else{//自己
        //修改资料按钮
        self.alterMessageButton.hidden = NO;
        
        //获取finishFlag判断红点显示隐藏
        if ([_layout.data.alterMessage integerValue] == 0) {//未完善资料
            self.redDot.hidden = NO;
        }
        if ([_layout.data.alterMessage integerValue] == 1) {//已完善资料
            self.redDot.hidden = YES;
        }
    }
    
    self.authImage.hidden = _layout.type == SEGEnumHeadPartViewType_NO;
    [self setupUIWithIsAuth:_layout.type == SEGEnumHeadPartViewType_YES];
}

// 布局认证和未认证的公共UI
- (void)setupUIWithIsAuth:(BOOL)isAuth {
    //头像
    [self.iconButton mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).mas_offset(30);
        make.top.equalTo(self.describeLabel.mas_bottom).mas_offset(15);
        make.width.height.mas_equalTo(90);
    }];
    
    //性别
    [self.sexImage mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.iconButton.mas_right).mas_offset(6);
        make.bottom.equalTo(self.iconButton.mas_bottom);
        make.width.height.mas_equalTo(15);
    }];
    
    // 用户是否认证
    if (isAuth) { // 已认证
        //认证
        [self.authImage mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.iconButton.mas_right).mas_offset(6);
            make.bottom.equalTo(self.iconButton.mas_bottom);
            make.width.height.mas_equalTo(15);
        }];
        
        //等级
        [self.levelImage mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.authImage.mas_right).mas_offset(6);
            make.bottom.equalTo(self.iconButton.mas_bottom);
            make.width.height.mas_equalTo(15);
        }];
        
    } else { // 未认证
        //等级
        [self.levelImage mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.iconButton.mas_right).mas_offset(6);
            make.bottom.equalTo(self.iconButton.mas_bottom);
            make.width.height.mas_equalTo(15);
        }];
    }
    
    //地址
    [self.addressLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).mas_offset(30);
        make.top.equalTo(self.iconButton.mas_bottom).mas_offset(13);
        make.height.mas_equalTo(_layout.addressLabelHeight);
        make.width.mas_equalTo(kHomePageHeadViewWidth - 30 - 30);
    }];
    
    //白色方块背景
    [self.whiteBGView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).mas_offset(7.5);
        make.right.equalTo(self).mas_offset(-7.5);
        make.height.mas_equalTo(118);
        make.bottom.equalTo(self.addressLabel.mas_bottom).mas_offset(20);
    }];
    
    //修改信息
    [self.alterMessageButton mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.whiteBGView.mas_right).mas_offset(-15 - 7.5);
        make.top.equalTo(self.whiteBGView.mas_top).mas_offset(20 + 7.5);
        make.height.width.mas_equalTo(20);
    }];
    
    //红点
    [self.redDot mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.alterMessageButton.mas_right);
        make.top.equalTo(self.alterMessageButton.mas_top);
        make.height.width.mas_equalTo(5);
    }];
    
    
    self.addressLabel.textAlignment = NSTextAlignmentLeft;
    self.addressLabel.textVerticalAlignment = SEGTextVerticalAlignmentCenter;
}

- (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection {
    [_alterMessageButton setImage:[UIImage imageWithContentsOfFile:SEGPicName(@"icon_personal_edit")] forState:UIControlStateNormal];
    [_alterMessageButton setImage:[UIImage imageWithContentsOfFile:SEGPicName(@"icon_personal_edit")] forState:UIControlStateHighlighted];
}

#pragma mark - getter

-(UIImageView *)sexImage{
    if (!_sexImage) {
        _sexImage = [[UIImageView alloc]init];
        _sexImage.image = [UIImage imageWithContentsOfFile:SEGPicName(_layout.data.sex)];
        _sexImage.contentMode = UIViewContentModeScaleAspectFill;
        _sexImage.hidden = YES;
        // if (kDebug)_sexImage.backgroundColor = kSegRandomColor;
    }
    return _sexImage;
}

-(UIImageView *)authImage{
    if (!_authImage) {
        _authImage = [[UIImageView alloc]init];
        _authImage.image = [UIImage imageWithContentsOfFile:SEGPicName(_layout.data.authentication)];
        _authImage.contentMode = UIViewContentModeScaleAspectFill;
        // if (kDebug)_authImage.backgroundColor = kSegRandomColor;
    }
    return _authImage;
}

-(UIImageView *)levelImage{
    if (!_levelImage) {
        _levelImage = [[UIImageView alloc]init];
       [_levelImage setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",kClientImageRootURL,_layout.data.level]]];
        _levelImage.contentMode = UIViewContentModeScaleAspectFill;
        _levelImage.layer.masksToBounds = YES;
        // if (kDebug)_levelImage.backgroundColor = kSegRandomColor;
    }
    return _levelImage;
}

-(UIImageView *)imageBGView{
    if (!_imageBGView) {
        _imageBGView = [[UIImageView alloc]init];
        _imageBGView.image = [UIImage imageWithContentsOfFile:SEGPicName(@"pic_personal_bg")];
        _imageBGView.contentMode = UIViewContentModeScaleAspectFill;
        _imageBGView.layer.masksToBounds = YES;
        // if (kDebug)_imageBGView.backgroundColor = kSegRandomColor;
    }
    return _imageBGView;
}

-(UIImageView *)whiteBGView{
    if (!_whiteBGView){
        _whiteBGView = [[UIImageView alloc]init];
        UIImage *image = [UIImage imageWithContentsOfFile:SEGPicName(@"pic_personal_bg2")];
        image = [image imageWithRenderingMode: UIImageRenderingModeAlwaysTemplate];
        _whiteBGView.image = image;
        _whiteBGView.tintColor = kColor_W3;
        // if (kDebug)_addressLabel.backgroundColor = kSegRandomColor;
    }
    return _whiteBGView;
}

-(SEGLabel *)describeLabel{
    if (!_describeLabel) {
        _describeLabel = [[SEGLabel alloc]init];
        _describeLabel.userInteractionEnabled = NO;
        _describeLabel.textAlignment = NSTextAlignmentLeft;
        _describeLabel.textVerticalAlignment = SEGTextVerticalAlignmentCenter;
        _describeLabel.displaysAsynchronously = YES;
        _describeLabel.ignoreCommonProperties = YES;//YES会影响系统属性
        _describeLabel.fadeOnHighlight = NO;
        _describeLabel.numberOfLines = 2;
        _describeLabel.fadeOnAsynchronouslyDisplay = NO;
        _describeLabel.fadeOnAsynchronouslyDisplay = NO;
        _describeLabel.lineBreakMode = NSLineBreakByTruncatingTail;
        // if (kDebug)_describeLabel.backgroundColor = kSegRandomColor;
    }
    return _describeLabel;
}

-(SEGLabel *)nickNameLabel{
    if (!_nickNameLabel) {
        _nickNameLabel = [[SEGLabel alloc]init];
        _nickNameLabel.userInteractionEnabled = NO;
        _nickNameLabel.textAlignment = NSTextAlignmentLeft;
        _nickNameLabel.textVerticalAlignment = SEGTextVerticalAlignmentCenter;
        _nickNameLabel.displaysAsynchronously = YES;
        _nickNameLabel.ignoreCommonProperties = YES;//YES会影响系统属性
        _nickNameLabel.fadeOnHighlight = NO;
        _nickNameLabel.numberOfLines = 1;
        _nickNameLabel.fadeOnAsynchronouslyDisplay = NO;
        _nickNameLabel.fadeOnAsynchronouslyDisplay = NO;
        _nickNameLabel.lineBreakMode = NSLineBreakByTruncatingTail;
        // if (kDebug)_nickNameLabel.backgroundColor = kSegRandomColor;
    }
    return _nickNameLabel;
}


-(SEGLabel *)addressLabel{
    if (!_addressLabel) {
        _addressLabel = [[SEGLabel alloc]init];
        _addressLabel.userInteractionEnabled = NO;
        _addressLabel.textAlignment = NSTextAlignmentLeft;
        _addressLabel.textVerticalAlignment = SEGTextVerticalAlignmentCenter;
        _addressLabel.displaysAsynchronously = YES;
        _addressLabel.ignoreCommonProperties = YES;//YES会影响系统属性
        _addressLabel.fadeOnHighlight = NO;
        _addressLabel.numberOfLines = 1;
        _addressLabel.fadeOnAsynchronouslyDisplay = NO;
        _addressLabel.fadeOnAsynchronouslyDisplay = NO;
        // if (kDebug)_addressLabel.backgroundColor = kSegRandomColor;
    }
    return _addressLabel;
}


-(UIButton *)iconButton{
    if (!_iconButton) {
        _iconButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _iconButton.layer.cornerRadius = 5.0;
        _iconButton.layer.masksToBounds = YES;
        [_iconButton setImage:[UIImage imageWithContentsOfFile:SEGPicName(@"female_heading180")] forState:UIControlStateNormal];
        // if (kDebug)_iconButton.backgroundColor = kSegRandomColor;
        [_iconButton addTarget:self action:@selector(iconButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return  _iconButton;
}

-(UIButton *)alterMessageButton{
    if (!_alterMessageButton) {
        _alterMessageButton = [UIButton buttonWithType:UIButtonTypeCustom];
        
        [_alterMessageButton setImage:[UIImage imageWithContentsOfFile:SEGPicName(@"icon_personal_edit")] forState:UIControlStateNormal];
        [_alterMessageButton setImage:[UIImage imageWithContentsOfFile:SEGPicName(@"icon_personal_edit")] forState:UIControlStateHighlighted];

        // if (kDebug)_alterMessageButton.backgroundColor = kSegRandomColor;
        [_alterMessageButton addTarget:self action:@selector(alterMessageButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return  _alterMessageButton;
}

-(UIImageView *)redDot{
    if (!_redDot) {
        _redDot = [[UIImageView alloc]init];
        _redDot.image = [UIImage imageWithContentsOfFile:SEGPicName(@"tab_red_round")];
        _redDot.contentMode = UIViewContentModeScaleAspectFill;
        // if (kDebug)_redDot.backgroundColor = kSegRandomColor;
    }
    return _redDot;
}

-(void)iconButtonClick:(UIButton*)sender{
    __weak typeof(self) _self = self;
    if (_self.delegate && [_self.delegate respondsToSelector:@selector(SEGProtocoliconButton:)]) {
        [_self.delegate SEGProtocoliconButton:_self.iconButton];
    }
}

-(void)alterMessageButtonClick:(UIButton*)sender{
    __weak typeof(self) _self = self;
    if (_self.delegate && [_self.delegate respondsToSelector:@selector(SEGProtocolalterMessageButton:)]) {
    [_self.delegate SEGProtocolalterMessageButton:_self.alterMessageButton];
    }
}

@end
